package com.weisy.signin.ui.activity;

import android.os.Handler;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.hjq.toast.ToastUtils;
import com.weisy.signin.BuildConfig;
import com.weisy.signin.R;
import com.weisy.signin.base.BaseAcativity;
import com.weisy.signin.bean.Student;
import com.weisy.signin.bean.Teacher;
import com.weisy.signin.comm.Comm;
import com.weisy.signin.utils.GsonUtils;
import com.weisy.signin.utils.SpUtils;
import com.weisy.signin.widget.LoadingDialog;

import butterknife.BindView;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;

/**
 * @author weisy
 * @function 登录
 * @date 2019/3/20 23:45
 */
@Route(path = Comm.ROUTE_LOGIN)
public class LoginActivity extends BaseAcativity {

    @BindView(R.id.edit_login_id)
    EditText editId;
    @BindView(R.id.edit_login_pass)
    EditText editPass;
    @BindView(R.id.radio_type_student)
    RadioButton radioStudent;
    @BindView(R.id.radio_type_teacher)
    RadioButton radioTeacher;

    @BindView(R.id.txt_login_version)
    TextView txtVesion;

    private int userType = 1;
    private Student student;
    private Teacher teacher;

    @Override
    public void findView() {
        setContentView(R.layout.activity_login);
    }

    @Override
    public void initView() {
        isResume = true;
        txtVesion.setText(BuildConfig.VERSION_NAME);
    }

    @Override
    public void initData() {
        if (SpUtils.contains(context, Comm.SP_TYPE)) {
            userType = (int) SpUtils.get(context, Comm.SP_TYPE, 1);
            String person = (String) SpUtils.get(context, Comm.SP_PERION, "{}");
            if (userType == 1) {
                student = GsonUtils.str2Bean(person, Student.class);
                editId.setText(student.getStudentId());
                editPass.setText(student.getPassword());
                radioStudent.setChecked(true);
            } else {
                teacher = GsonUtils.str2Bean(person, Teacher.class);
                editId.setText(teacher.getTeachId());
                editPass.setText(teacher.getPassword());
                radioTeacher.setChecked(true);
            }
        }
    }

    @OnCheckedChanged({R.id.radio_type_student, R.id.radio_type_teacher})
    public void onRadioCheck(CompoundButton view, boolean ischanged) {
        switch (view.getId()) {
            case R.id.radio_type_student:
                if (ischanged) {
                    userType = 1;
                    editId.setHint("请输入学生学号");
                }
                break;
            case R.id.radio_type_teacher:
                if (ischanged) {
                    userType = 2;
                    editId.setHint("请输入教师工号");
                }
                break;
        }
    }

    @OnClick({R.id.txt_login_register, R.id.btn_login_submit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.txt_login_register:
                ARouter.getInstance().build(Comm.ROUTE_REGISTER).withInt("userType", userType).navigation();
                break;
            case R.id.btn_login_submit:
                login();
                break;
        }
    }

    private void login() {
        String id = editId.getText().toString().trim();
        String pass = editPass.getText().toString().trim();
        if (TextUtils.isEmpty(id) || TextUtils.isEmpty(pass)) {
            ToastUtils.show((userType == 1 ? "学号" : "工号") + "或者密码不能为空");
            return;
        }
        final LoadingDialog loadingDialog = new LoadingDialog.Builder(context)
                .setBgDark(true)
                .setMsg("登录中...")
                .create();
        loadingDialog.show();

        new Handler().postDelayed(() -> {
            if (loadingDialog.isShowing()) loadingDialog.dismiss();
            if (userType == 1) {
                //学生登录
                if (null == student) {
                    ToastUtils.show("该用户未注册");
                    return;
                }
                if (id.equals(student.getStudentId()) && pass.equals(student.getPassword())) {
                    ToastUtils.show("登录成功");
                    SpUtils.put(context, Comm.SP_LOGIN, true);
                    ARouter.getInstance().build(Comm.ROUTE_STUDENT).navigation();
                    finish();
                } else {
                    ToastUtils.show("学号或密码错误");
                }
            } else {
                //老师登录
                if (null == teacher) {
                    ToastUtils.show("该用户未注册");
                    return;
                }
                if (id.equals(teacher.getTeachId()) && pass.equals(teacher.getPassword())) {
                    ToastUtils.show("登录成功");
                    SpUtils.put(context, Comm.SP_LOGIN, true);
                    ARouter.getInstance().build(Comm.ROUTE_TEACHER).navigation();
                    finish();
                } else {
                    ToastUtils.show("工号或密码错误");
                }
            }
        }, 2000);
    }

    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出程序",
                        Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                android.os.Process.killProcess(android.os.Process.myPid());   //获取PID
                System.exit(0);
                finish();
            }
        }
        return true;
    }
}
